package com.suyun.controller;

import com.alibaba.fastjson.JSON;
import com.suyun.domain.Order;
import com.suyun.domain.Product;
import com.suyun.service.OrderService;
import com.suyun.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

//@RestController
@Slf4j
public class OrdertController2 {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;


    //改用fegin
    @RequestMapping("/order/product/create/{pid}")
    public Order createOrder(@PathVariable("pid") Integer pid){
        log.info("创建一个pid为{}的订单",pid);

        log.info("rest",restTemplate);


        //直接使用服务名访问 service-producter
        Product product = productService.findByPid(pid);
        log.info("调用微服务查询商品为{}", JSON.toJSONString(product));

        //模拟调用商品微服务需要两秒
        try {
            Thread.sleep(2000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());

        order.setNumber(1);

        /*orderService.createOrder(order);*/
        log.info("创建订单成功{},pid={}",JSON.toJSONString(order),pid);
        return order;
    }

    @RequestMapping("order/message")
    public String message() {
        return "测试高并发";
    }
}
